;(function($,jq){
	"use strict";

	var dHeight = $(document).height(),
		wHeight = $(window).height(),
		scrollTop = 0, //滚动条拉伸的距离
		mHeight = 1, //滚动条离底部的距离至少5px
		flag = false, //是否滑动到页面底部
		curType = 'total', //查询的记录类型，默认为全部
		lastMonth = "", 
		eOuter = $('.outer');

	// jq('.inner').scroll(function(){
	// 	scrollTop = $(this).scrollTop();
	// 	dHeight = $(this).get(0).scrollHeight;
	// 	//console.log($(this).height()+"-"+$(this).get(0).scrollHeight+"-"+$(this).scrollTop());
	// 	console.log(wHeight+'-'+scrollTop+"-"+dHeight+'-'+(dHeight-wHeight-scrollTop)
	// 			+'-'+(wHeight + scrollTop >= dHeight - mHeight));
	// 	if(wHeight + scrollTop >= dHeight - mHeight){
	// 		flag = true;
	// 	}else{
	// 		flag = false;
	// 	}
	// });

	$(".topnav li").tap(function(e){
		var that = $(this),
			oType = ['total','recharge','trade'], //tab类型
			type = $(this).data("type"), //当前tab内容
			_type = "";

			if(oType.indexOf(type)>-1){
				//切换导航样式
				that.addClass("current").siblings().removeClass("current");

				for(var i in oType) {
					_type = "." + oType[i] + "-record";
					type === oType[i] ? $(_type).removeClass("dps-n") : $(_type).addClass("dps-n");
				}

				//ajax查询
					
			}

	});

	// dropload
	var dropload = $('.inner').dropload({
	    domUp : {
	        domClass   : 'dropload-up',
	        domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
	        domUpdate  : '<div class="dropload-update">↑释放更新</div>',
	        domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
	    },
	    domDown : {
	        domClass   : 'dropload-down',
	        domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
	        domUpdate  : '<div class="dropload-update">↓释放加载</div>',
	        domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
	    },
	    loadUpFn : function(me){
	    	//刷新页面
	    	
	    	me.resetload();
	    },
	    loadDownFn : function(me){
			//alert(wHeight+'-'+scrollTop+'-'+dHeight);
	    	if(flag){
	    		var json = [{
							"month": "6月",
							"data":[
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								},
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								},
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								}
							]
						},{
							"month": "2013年7月",
							"data": [
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								},
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								},
								{
									cardNo: "6-20",
									payType:"银联充值",
									banlance: "余额账户",
									txnDate:"+500.00",
									txnStat: "成功"
								}
							]
						}
					];
				var tObj = null,//月份总数据
					nLen = 0,
					frag = document.createDocumentFragment(); // 创建文档碎片
	    		if(Object.prototype.toString.call(json)==="[object Array]"){
					nLen = json.length;
					//遍历最近三月数据
					for(var i = 0; i < nLen; i++){
						tObj = json[i];
						frag.appendChild(createList(tObj)[0]);
					}
					$(frag).appendTo($("."+curType+"-record"));
				}
	    	}
	    	me.resetload();
	    }
	});
	
	function createList(obj){
		var tData = null, //每月单条数据
			nCount = 0,
			nLen = 0,
			eDl = null,
			eDt = null,
			eDd = null,
			eDiv = null,
			eLabel = null;
		try{
			if(lastMonth!=obj.month){
				lastMonth = obj.month;
				eDl = jq("<dl></dl>").attr("id",lastMonth.replace("年","-").replace("月",""));
				eDt = jq("<dt></dt>");
				eDt.text(obj.month).appendTo(eDl);
			}else{
				eDl = $("#"+lastMonth.replace("年","-").replace("月",""));
			}
			
			tData = obj.data;
			console.log(lastMonth);
			if(Object.prototype.toString.call(tData)==="[object Array]"){
				nLen = tData.length;
				//遍历每月记录数
				for(var j = 0; j < nLen; j++){
					nCount = 0;
					eDd = jq("<dd></dd>");
					//获取对象属性值
					for(var o in tData[j]){
						if(tData.hasOwnProperty(j)){
							switch(nCount){
								case 0 : 
									eDiv = jq("<div></div>");
									eLabel = jq("<label></label>");
									eLabel.addClass("type").text(tData[j][o]).appendTo(eDiv);
									break;
								case 1 :
									eLabel = jq("<label></label>");
									eLabel.addClass("way").text(tData[j][o]).appendTo(eDiv);
									break;
								case 2 : 
									eLabel = jq("<label></label>");
									eLabel.addClass("num").text(tData[j][o]).appendTo(eDiv);
									eDiv.appendTo(eDd);
									eDiv = null;
									break;
								case 3 : 
									eDiv = jq("<div></div>");
									eLabel = jq("<label></label>");
									eLabel.addClass("time").text(tData[j][o]).appendTo(eDiv);
									break;
								case 4 :
									eLabel = jq("<label></label>");
									eLabel.addClass("status").text(tData[j][o]).appendTo(eDiv);
									eDiv.appendTo(eDd);
									break;
							}
							nCount++;
						}
					}
					eDd.appendTo(eDl);
				}
			}
		}catch(e){
			return null;
		}
		return eDl;
	}

	function toggleTab(){
		var that = $("#id"),
			type = that.val(), //当前tab内容
			oType = ['total','recharge','trade']; //tab类型

		if(type){
			_type = type==="total"?oType[1]:oType[0]; //待隐藏tab内容
			if(oType.indexOf(type)>-1){
				//切换导航样式
				that.addClass("current").siblings().removeClass("current");

				curType = type;

				//切换'交易记录'、'充值记录'查询列表
				$("."+type+"-record").removeClass("dps-n");
				$("."+_type+"-record").addClass("dps-n");
			}
		}
	}

})(Zepto);